<?php
namespace Home\Model;
use  Think\Model;

class CutnumModel extends Model{

    public function check1($pid,$id,$level){

        $return_num = $this->checknum($pid,$id,$level);

        if($return_num == -1){

            return 2;
        }

        if($level == 2){
            // 升级为vip 合伙人减去一个vip名额
            $num['userId'] = $pid;
            $num['geterId'] = $id;
            $num['num'] = -1;

            $res_num = M('cutnum')->add($num);

            $new_return_num = $this->checknum($pid,$id,$level);

            if($new_return_num == -1){

                return 2;// 名额不足
            }

            // 将该用户级别改为vip用户
            if($res_num){

                $list_where['id'] = $id;
                $list_user['level'] = 2;
                $list_user['pid'] = $pid;

                // 将详情表信息转移到vip详情表中
                $user_detail = M('detail1')->where("userId = '".$id."'")->find();

                unset($user_detail['id']);
                $res_detail = M('detail2')->add($user_detail);
                $list_user['detailId'] = $res_detail;

                // 将图片表转移
                $user_pic = M('pic1')->where("userId = '".$id."'")->find();

                unset($user_pic['id']);
                $res_pic = M('pic2')->add($user_pic);

                $list_user['picId'] = $res_pic;

                // 修改user表
                $res_user_save = M('user')->where("id = '".$id."'")->save($list_user);

            }

        }else if($level == 3){

            // 如果 该合伙人是梦想合伙人
            $get_user = M('user')->where("id = '".$pid."'")->find();

            if($get_user['level'] == 4){

                if($return_num < 20){

                    $executestatus = $this->execute($id,$pid,$level);

                    return 2;

                }else{

                    M('cutnum')->startTrans();  // 开启事务
                    // TODO :: 梦想合伙人减去20个名额
                    $num['userId'] = $pid;
                    $num['geterId'] = $id;
                    $num['num'] = -20;
                    $res_num1 = M('cutnum')->add($num);

                    $num1['userId'] = $id;  // 升级为天使合伙人增加20个名额
                    $num1['geterId'] = $pid;
                    $num1['num'] = 20;

                    $res_num2 = M('cutnum')->add($num1);

                    if($res_num1 && $res_num2){

                        M('cutnum')->commit();  // 提交事务
                    }else{

                        M('cutnum')->rollback(); // 回滚事务
                    }

                    if($res_num2){

                        $num2['userId'] = $id; // 强制升级为vip 减少一个名额
                        $num2['geterId'] = $id;
                        $num2['num'] = -1;

                        $res_num3 = M('cutnum')->add($num2);

                        if($res_num3){

                            $this->savestatus($id,$level,$pid);
                        }
                    }

                }
            }else{

                return 3;// 错误
            }

        }
    }


    public function savestatus($id,$level,$pid){

        $list_where['id'] = $id;
        $list_user['level'] = 3;
        $list_user['pid'] = $pid;

        // 将详情表信息转移到vip详情表中
        $user_detail = M('detail1')->where("userId = '".$id."'")->find();

        unset($user_detail['id']);

        $res_detail = M('detail3')->add($user_detail);
        $list_user['detailId'] = $res_detail;

        // 将图片表转移
        $user_pic = M('pic1')->where("userId = '".$id."'")->find();

        unset($user_pic['id']);
        $res_pic = M('pic3')->add($user_pic);

        $list_user['picId'] = $res_pic;

        $msg_log= APP_PATH.'1.log';
        monitor_log($msg_log,$list_user,FILE_APPEND);

        // 修改user表
        $res_user_save = M('user')->where("id = '".$id."'")->save($list_user);

    }


    public function check($pid,$id,$level,$this_int){

        $get_user_result = M('user')->where("id = '".$pid."'")->find();

        $num = M('cutnum')->where('userId = "'.$id.'"')->find();

        $return_num = $this->checknum($pid,$id,$level,$this_int);

        if($return_num == -1){

            return 2;
        }

        if($get_user_result['level'] != 4){

            return 1 ;// 参数有误
        }

        if(!$num){

            $rulenum = 19;
        }else{

            $rulenum = 20;
        }

        $msg_log = APP_PATH.'cutnum2.log';
        monitor_log($msg_log,array('r_num'=>$return_num,'rulenum'=>$rulenum),FILE_APPEND);

        if($return_num < $rulenum){

            $executestatus = $this->execute($id,$pid,$level,$this_int);

            return 2;


        }else{

            M('num')->startTrans();  // 开启事务
            // TODO :: 梦想合伙人减去20个名额
            $num1['userId'] = $pid;
            $num1['geterId'] = $id;
            $num1['num'] = -$rulenum;
            $res_num1 = M('cutnum')->add($num1);

            $num2['userId'] = $id;  // 升级为天使合伙人增加20个名额
            $num2['geterId'] = $pid;
            $num2['num'] = $rulenum;

            $msg_log = APP_PATH.'cutnum.log';
            monitor_log($msg_log,array('num1'=>$num1,'num2'=>$num2),FILE_APPEND);

            $res_num2 = M('cutnum')->add($num2);

            if($res_num1 && $res_num2){

                M('num')->commit();  // 提交事务
            }else{

                M('num')->rollback(); // 回滚事务
            }

            if($res_num2){
                $this->savestatus($id,$level,$pid);
            }
        }
    }


    public function checknum($pid,$id,$level,$this_int){

        $num = M('cutnum')->where('userId = "'.$pid.'"')->field('sum(num) as number')->select();

        $msg_log = APP_PATH.'cutnum3.log';
        monitor_log($msg_log,array('num'=>$num),FILE_APPEND);

        $number = $num[0]['number'];
        if($number <= 7 && $number >= 0){
            // TODO :: 调用公告
            $create_notice['noticeId'] = 1;
            $create_notice['userId'] = $pid;
            $create_notice['time'] = time();

            $res_notice = M('notice')->add($create_notice);

        }else if($number == -1){
            // 名额不足， 执行操作
            $executestatus = $this->execute($id,$pid,$level,$this_int);

                return -1;
                // $data = genBody('1','升级失败，请联系您的上级');
                // $this->ajaxReturn($data,'JSON');


        }

        return $number;

    }

    public function execute($id,$pid,$level,$this_int){

        // TODO :: 公告 梦想合伙人可以重新购买梦想合伙人权限 或者不升级
        // 发布公告  请操作
        $arr_notice['noticeId'] = 2;
        $arr_notice['userId'] = $pid;
        $arr_notice['time'] = time();

        $user_result = M('notice')->add($arr_notice);


        // 该合伙人下级的所有VIP会员也不允许邀请用户
        $user_status['status'] = 2;
        $res_user_status = M('user')->where("pid = '".$pid."' and level = 2")->save($user_status);

        // 梦想合伙人名额不足 进入判定状态  将该升级用户添加进入status表中，如果24小时后，梦想合伙人没有任何操作，则将该用户由平台提交给其他梦想合伙人。
        $arr_status['userId'] = $id;
        $arr_status['pid'] = $pid;
        $arr_status['level'] = $level;
        $arr_status['starTime'] = time();
        $arr_status['endTime'] = time()+24*3600;
        $arr_status['status'] = 1;
        $res_status = M('status')->add($arr_status);

        // 将pid 的状态设置为2  名额超限状态  存入userstatus表中
        $arr_userstatus['userId']  = $pid;
        $arr_userstatus['status'] = 2;
        $res_user_level = M('user')->where("id = '".$pid."'")->find();
        $table = 'userstatus'.$res_user_level['level'];
        $res_userstatus = M($table)->where("userId = '".$pid."'")->find();
        if($res_userstatus){
            $res_save_user = M($table)->where("userId = '".$pid."'")->save($arr_userstatus);
        }else{
            $res_add_user = M($table)->add($arr_userstatus);
        }

        // 将插入金钱表的记录state 修改为2  表示不确定状态
        if($this_int){
            $money_table = 'repeartmoney';
        }else{

            $money_table = 'updatemoney';
        }
        $money_state['state'] = 2;// 不确定状态

        $update_money = M($money_table)->where("userId = '".$id."'")->save($money_state);

        $update_money = M($money_table)->where("userId = '".$pid."'")->save($money_state);

        return 1;

    }
}